package com.hyg.lib_common.ui.adpter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.hyg.lib_common.DataModel.testting.TesttingAnswer;
import com.hyg.lib_common.R;
import com.hyg.lib_common.listener.RCVchlickListener;

import java.util.ArrayList;

/**
 * @author: Administrator
 * @date: 2021/3/17
 */
public class RadioButtonAnswerHorAdapter extends RecyclerView.Adapter<RadioButtonAnswerHorAdapter.ViewHolder> {
    ArrayList<TesttingAnswer> list;
    Context context;
    RCVchlickListener listener;

    public RadioButtonAnswerHorAdapter(Context context, ArrayList<TesttingAnswer> list, RCVchlickListener listener) {
        this.context = context;
        this.list = list;
        this.listener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_radiobutton_answer_hor, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.answerBtn.setText(list.get(position).getAnswer());
        if (list.get(position).isChoose()) {
            holder.answerBtn.setBackground(context.getResources().getDrawable(R.drawable.cor_main_voilet_solid_small));
            holder.answerBtn.setTextColor(context.getResources().getColor(R.color.white));
        } else {
            holder.answerBtn.setBackground(context.getResources().getDrawable(R.drawable.cor_gray_stroke_small));
            holder.answerBtn.setTextColor(context.getResources().getColor(R.color.mainMusicColor));
        }

        holder.answerBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!list.get(position).isChoose()) {
                    for (int i = 0; i < list.size(); i++) {
                        list.get(i).setChoose(false);
                    }
                    list.get(position).setChoose(true);
                    notifyDataSetChanged();
                }
                if(listener!=null){
                    listener.onItemClickListener();
                }
            }
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        Button answerBtn;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            answerBtn = itemView.findViewById(R.id.btn_answer_btn);
        }
    }


}
